package com.itheima.course.design.ui;

import com.itheima.course.design.entity.Student;
import com.itheima.course.design.service.StudentService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainUI extends JFrame {
    private StudentService studentService = new StudentService();

    public MainUI() {
        initUI();
    }

    private void initUI() {
        setTitle("学生选课系统");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 1));

        JButton loginButton = new JButton("学生登录");
        JButton registerButton = new JButton("学生注册");
        JButton manageCoursesButton = new JButton("管理课程");
        JButton exitButton = new JButton("退出系统");

        panel.add(loginButton);
        panel.add(registerButton);
        panel.add(manageCoursesButton);
        panel.add(exitButton);

        add(panel);

        // 登录按钮事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showLoginDialog();
            }
        });

        // 注册按钮事件
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showRegisterDialog();
            }
        });

        // 管理课程按钮事件
        manageCoursesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new CourseManagementUI().setVisible(true);
            }
        });

        // 退出按钮事件
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    // 显示登录对话框
    private void showLoginDialog() {
        JTextField studentIdField = new JTextField(20);
        JPasswordField passwordField = new JPasswordField(20);

        JPanel panel = new JPanel(new GridLayout(2, 2));
        panel.add(new JLabel("学号:"));
        panel.add(studentIdField);
        panel.add(new JLabel("密码:"));
        panel.add(passwordField);

        int result = JOptionPane.showConfirmDialog(this, panel, "学生登录", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            String studentId = studentIdField.getText();
            String password = new String(passwordField.getPassword());

            Student student = studentService.login(studentId, password);
            if (student != null) {
                JOptionPane.showMessageDialog(this, "登录成功！");
                new StudentUI(student).setVisible(true);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "学号或密码错误！", "登录失败", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    // 显示注册对话框
    private void showRegisterDialog() {
        JTextField nameField = new JTextField(20);
        JTextField studentIdField = new JTextField(20);
        JPasswordField passwordField = new JPasswordField(20);

        JPanel panel = new JPanel(new GridLayout(3, 2));
        panel.add(new JLabel("姓名:"));
        panel.add(nameField);
        panel.add(new JLabel("学号:"));
        panel.add(studentIdField);
        panel.add(new JLabel("密码:"));
        panel.add(passwordField);

        int result = JOptionPane.showConfirmDialog(this, panel, "学生注册", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            String name = nameField.getText();
            String studentId = studentIdField.getText();
            String password = new String(passwordField.getPassword());

            if (name.isEmpty() || studentId.isEmpty() || password.isEmpty()) {
                JOptionPane.showMessageDialog(this, "所有字段都是必填项！", "注册失败", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Student student = new Student();
            student.setName(name);
            student.setStudentId(studentId);
            student.setPassword(password);

            boolean success = studentService.register(student);
            if (success) {
                JOptionPane.showMessageDialog(this, "注册成功！");
            } else {
                JOptionPane.showMessageDialog(this, "注册失败，请重试！", "注册失败", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new MainUI().setVisible(true);
        });
    }
}